1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class Supplementary {
32
33 public static void main(String[] args) {
34 test1();
35 test2();
36 test3();
37 test4();
38 test5();
39 test6();
40 }
41
42
43
44
45
46 static final String[] input = {
47
48
49 "abc\uD800\uDC00def\uD800\uD800ab\uD800\uDC00cdefa\uDC00bcdef",
50
51
52 "\uD800defg\uD800hij\uD800\uDC00klm\uDC00nop\uDC00\uD800rt\uDC00",
53
54
55 "\uDC00abcd\uDBFF\uDFFFefgh\uD800\uDC009ik\uDC00\uDC00lm\uDC00no\uD800",
56
57
58 "\uD800\uDC00!#$\uD800%&\uD800\uDC00;+\uDC00<>;=^\uDC00\\@\uD800\uDC00",
59
60
61
62
63 "\uDB40\uDE00abc\uDE01\uDB40de\uDB40\uDE02f\uDB40\uDE03ghi\uDB40\uDE02",
64 };
65
66
67
68
69
70
71
72
73
74 static final int[][] golden1 = {
75 {'a', 0xD800, 0xDC00, 0x10000, 0xE0200},
76 {0xD800, 0x10000, 'g', 0xDC00, 0xE0202},
77 {'f', 0xDC00, 0xD800, 0xDC00, 0xDE02},
78 };
79
80
81
82
83 static void test1() {
84
85 for (int i = 0; i < input.length; i++) {
86 StringBuffer sb = new StringBuffer(input[i]);
87
88
89
90
91 testCodePoint(At, sb, 0, golden1[0][i]);
92 testCodePoint(At, sb, 9, golden1[1][i]);
93 testCodePoint(At, sb, sb.length()-1, golden1[2][i]);
94
95
96
97
98 testCodePoint(At, sb, -1);
99 testCodePoint(At, sb, sb.length());
100 }
101 }
102
103
104
105
106
107
108
109
110
111 static final int[][] golden2 = {
112 {'a', 0xD800, 0xDC00, 0xD800, 0xDB40},
113 {0xD800, 'l', 0x10000, 0xDC00, 0xDB40},
114 {'f', 0xDC00, 0xD800, 0x10000, 0xE0202},
115 };
116
117
118
119
120 static void test2() {
121
122 for (int i = 0; i < input.length; i++) {
123 StringBuffer sb = new StringBuffer(input[i]);
124
125
126
127
128 testCodePoint(Before, sb, 1, golden2[0][i]);
129 testCodePoint(Before, sb, 13, golden2[1][i]);
130 testCodePoint(Before, sb, sb.length(), golden2[2][i]);
131
132
133
134
135 testCodePoint(Before, sb, 0);
136 testCodePoint(Before, sb, sb.length()+1);
137 }
138 }
139
140
141
142
143
144
145
146
147
148 static final String[] golden3 = {
149 "fedcb\uDC00afedc\uD800\uDC00ba\uD800\uD800fed\uD800\uDC00cba",
150 "\uDC00tr\uD800\uDC00pon\uDC00mlk\uD800\uDC00jih\uD800gfed\uD800",
151 "\uD800on\uDC00ml\uDC00\uDC00ki9\uD800\uDC00hgfe\uDBFF\uDFFFdcba\uDC00",
152 "\uD800\uDC00@\\\uDC00^=;><\uDC00+;\uD800\uDC00&%\uD800$#!\uD800\uDC00",
153
154
155 "\uDB40\uDE02ihg\uDB40\uDE03f\uDB40\uDE02ed\uDB40\uDE01cba\uDB40\uDE00",
156 };
157
158
159 static final String[][] testdata1 = {
160 {"a\uD800\uDC00", "\uD800\uDC00a"},
161 {"a\uDC00\uD800", "\uD800\uDC00a"},
162 {"\uD800\uDC00a", "a\uD800\uDC00"},
163 {"\uDC00\uD800a", "a\uD800\uDC00"},
164 {"\uDC00\uD800\uD801", "\uD801\uD800\uDC00"},
165 {"\uDC00\uD800\uDC01", "\uD800\uDC01\uDC00"},
166 {"\uD801\uD800\uDC00", "\uD800\uDC00\uD801"},
167 {"\uD800\uDC01\uDC00", "\uDC00\uD800\uDC01"},
168 {"\uD800\uDC00\uDC01\uD801", "\uD801\uDC01\uD800\uDC00"},
169 };
170
171
172
173
174 static void test3() {
175 for (int i = 0; i < input.length; i++) {
176 StringBuffer sb = new StringBuffer(input[i]).reverse();
177
178 check(!golden3[i].equals(new String(sb)),
179 "reverse() for <" + toHexString(input[i]) + ">",
180 sb, golden3[i]);
181 }
182
183 for (int i = 0; i < testdata1.length; i++) {
184 StringBuffer sb = new StringBuffer(testdata1[i][0]).reverse();
185
186 check(!testdata1[i][1].equals(new String(sb)),
187 "reverse() for <" + toHexString(testdata1[i][0]) + ">",
188 sb, testdata1[i][1]);
189 }
190 }
191
192
193
194
195 static void test4() {
196 for (int i = 0; i < input.length; i++) {
197 String s = input[i];
198 StringBuffer sb = new StringBuffer();
199 int c;
200 for (int j = 0; j < s.length(); j += Character.charCount(c)) {
201 c = s.codePointAt(j);
202 StringBuffer rsb = sb.appendCodePoint(c);
203 check(sb != rsb, "appendCodePoint returned a wrong object");
204 int sbc = sb.codePointAt(j);
205 check(sbc != c, "appendCodePoint(j) != c", sbc, c);
206 }
207 check(!s.equals(sb.toString()),
208 "appendCodePoint() produced a wrong result with input["+i+"]");
209 }
210
211
212 testAppendCodePoint(-1, IllegalArgumentException.class);
213 testAppendCodePoint(Character.MAX_CODE_POINT+1, IllegalArgumentException.class);
214 }
215
216
217
218
219
220
221
222
223 static void test5() {
224 for (int i = 0; i < input.length; i++) {
225 String s = input[i];
226 StringBuffer sb = new StringBuffer(s);
227 int length = sb.length();
228 for (int j = 0; j <= length; j++) {
229 int result = sb.codePointCount(j, length);
230 int expected = Character.codePointCount(sb, j, length);
231 check(result != expected, "codePointCount(input["+i+"], "+j+", "+length+")",
232 result, expected);
233 }
234 for (int j = length; j >= 0; j--) {
235 int result = sb.codePointCount(0, j);
236 int expected = Character.codePointCount(sb, 0, j);
237 check(result != expected, "codePointCount(input["+i+"], 0, "+j+")",
238 result, expected);
239 }
240
241
242 testCodePointCount(null, 0, 0, NullPointerException.class);
243 testCodePointCount(sb, -1, length, IndexOutOfBoundsException.class);
244 testCodePointCount(sb, 0, length+1, IndexOutOfBoundsException.class);
245 testCodePointCount(sb, length, length-1, IndexOutOfBoundsException.class);
246 }
247 }
248
249
250
251
252
253
254
255
256 static void test6() {
257 for (int i = 0; i < input.length; i++) {
258 String s = input[i];
259 StringBuffer sb = new StringBuffer(s);
260 int length = s.length();
261 for (int j = 0; j <= length; j++) {
262 int nCodePoints = Character.codePointCount(sb, j, length);
263 int result = sb.offsetByCodePoints(j, nCodePoints);
264 check(result != length,
265 "offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
266 result, length);
267 result = sb.offsetByCodePoints(length, -nCodePoints);
268 int expected = j;
269 if (j > 0 && j < length) {
270 int cp = sb.codePointBefore(j+1);
271 if (Character.isSupplementaryCodePoint(cp)) {
272 expected--;
273 }
274 }
275 check(result != expected,
276 "offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
277 result, expected);
278 }
279 for (int j = length; j >= 0; j--) {
280 int nCodePoints = Character.codePointCount(sb, 0, j);
281 int result = sb.offsetByCodePoints(0, nCodePoints);
282 int expected = j;
283 if (j > 0 && j < length) {
284 int cp = sb.codePointAt(j-1);
285 if (Character.isSupplementaryCodePoint(cp)) {
286 expected++;
287 }
288 }
289 check(result != expected,
290 "offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
291 result, expected);
292 result = sb.offsetByCodePoints(j, -nCodePoints);
293 check(result != 0,
294 "offsetBycodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
295 result, 0);
296 }
297
298
299 testOffsetByCodePoints(null, 0, 0, NullPointerException.class);
300 testOffsetByCodePoints(sb, -1, length, IndexOutOfBoundsException.class);
301 testOffsetByCodePoints(sb, 0, length+1, IndexOutOfBoundsException.class);
302 testOffsetByCodePoints(sb, 1, -2, IndexOutOfBoundsException.class);
303 testOffsetByCodePoints(sb, length, length-1, IndexOutOfBoundsException.class);
304 testOffsetByCodePoints(sb, length, -(length+1), IndexOutOfBoundsException.class);
305 }
306 }
307
308
309 static final boolean At = true, Before = false;
310
311 static void testCodePoint(boolean isAt, StringBuffer sb, int index, int expected) {
312 int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
313
314 check(c != expected,
315 "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
316 + sb + ">", c, expected);
317 }
318
319 static void testCodePoint(boolean isAt, StringBuffer sb, int index) {
320 boolean exceptionOccurred = false;
321
322 try {
323 int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
324 }
325 catch (StringIndexOutOfBoundsException e) {
326 exceptionOccurred = true;
327 }
328 check(!exceptionOccurred,
329 "codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
330 + sb + "> should throw StringIndexOutOfBoundsPointerException.");
331 }
332
333 static void testAppendCodePoint(int codePoint, Class expectedException) {
334 try {
335 new StringBuffer().appendCodePoint(codePoint);
336 } catch (Exception e) {
337 if (expectedException.isInstance(e)) {
338 return;
339 }
340 throw new RuntimeException("Error: Unexpected exception", e);
341 }
342 check(true, "appendCodePoint(" + toHexString(codePoint) + ") didn't throw "
343 + expectedException.getName());
344 }
345
346 static void testCodePointCount(StringBuffer sb, int beginIndex, int endIndex,
347 Class expectedException) {
348 try {
349 int n = sb.codePointCount(beginIndex, endIndex);
350 } catch (Exception e) {
351 if (expectedException.isInstance(e)) {
352 return;
353 }
354 throw new RuntimeException("Error: Unexpected exception", e);
355 }
356 check(true, "codePointCount() didn't throw " + expectedException.getName());
357 }
358
359 static void testOffsetByCodePoints(StringBuffer sb, int index, int offset,
360 Class expectedException) {
361 try {
362 int n = sb.offsetByCodePoints(index, offset);
363 } catch (Exception e) {
364 if (expectedException.isInstance(e)) {
365 return;
366 }
367 throw new RuntimeException("Error: Unexpected exception", e);
368 }
369 check(true, "offsetByCodePoints() didn't throw " + expectedException.getName());
370 }
371
372 static void check(boolean err, String msg) {
373 if (err) {
374 throw new RuntimeException("Error: " + msg);
375 }
376 }
377
378 static void check(boolean err, String s, int got, int expected) {
379 if (err) {
380 throw new RuntimeException("Error: " + s
381 + " returned an unexpected value. got "
382 + toHexString(got)
383 + ", expected "
384 + toHexString(expected));
385 }
386 }
387
388 static void check(boolean err, String s, StringBuffer got, String expected) {
389 if (err) {
390 throw new RuntimeException("Error: " + s
391 + " returned an unexpected value. got <"
392 + toHexString(new String(got))
393 + ">, expected <"
394 + toHexString(expected)
395 + ">");
396 }
397 }
398
399 private static String toHexString(int c) {
400 return "0x" + Integer.toHexString(c);
401 }
402
403 private static String toHexString(String s) {
404 StringBuffer sb = new StringBuffer();
405 for (int i = 0; i < s.length(); i++) {
406 char c = s.charAt(i);
407
408 sb.append(" 0x");
409 if (c < 0x10) sb.append('0');
410 if (c < 0x100) sb.append('0');
411 if (c < 0x1000) sb.append('0');
412 sb.append(Integer.toHexString(c));
413 }
414 sb.append(' ');
415 return sb.toString();
416 }
417 }